const router = require('koa-router')()
const {
  getList,
  getDetail,
  newMessage,
  updateBlog,
  countAppointment,
  totalAppointment,
  last
} = require('../controller/message')

const { SuccessModel, ErrorModel } = require('../model/resModel')

router.prefix('/api/message')

router.get('/mylist', async (ctx, next) => {
  const userId = ctx.query.userId
  const data = await getList(userId)
  ctx.body = new SuccessModel(data)
})

router.get('/mydetail', async (ctx, next) => {
  const id = ctx.query.id
  const data = await getDetail(id)
  ctx.body = new SuccessModel(data)
})

router.get('/last', async (ctx, next) => {
  const userId = ctx.query.userId
  const doctorId = ctx.query.doctorId  
  const data = await last(userId,doctorId)
  if(data){
	ctx.body = new SuccessModel(data) 
  }else{
	ctx.body = new ErrorModel('未查到数据') 
  }
})

router.get('/count', async (ctx, next) => { 
  const body = ctx.request.body
  const data = await countAppointment(body)
  ctx.body = new SuccessModel(data) 
  
})
 
router.post('/add',  async function (ctx, next) {
  const body = ctx.request.body 
  const data = await newMessage(body)
  if (data){
	ctx.body = new SuccessModel(data,"留言成功") 
  }else{
	ctx.body = new ErrorModel('留言失败') 
  }
})
 

module.exports = router
